//
//  NSArray+CrashIntercept.m
//  Pods
//
//  Created by 余思豪 on 2022/9/22.
//

#import <Foundation/Foundation.h>
#import <objc/message.h>

@implementation NSArray (CrashIntercept)

+ (void)load {
    // 一次性函数保证只交换了一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // __NSArrayI(多元素)
        Method old = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
        Method new = class_getInstanceMethod(self, @selector(YSH_objectAtIndex_NSArrayI:));
        if (old && new) {
            method_exchangeImplementations(old, new);
        }
        // __NSSingleObjectArrayI(单元素)
        old = class_getInstanceMethod(objc_getClass("__NSSingleObjectArrayI"), @selector(objectAtIndex:));
        new = class_getInstanceMethod(objc_getClass("__NSSingleObjectArrayI"), @selector(YSH_objectAtIndex_NSSingleObjectArrayI:));
        if (old && new) {
            method_exchangeImplementations(old, new);
        }
        // __NSArray0(无元素)
        old = class_getInstanceMethod(objc_getClass("__NSArray0"), @selector(objectAtIndex:));
        new = class_getInstanceMethod(objc_getClass("__NSArray0"), @selector(YSH_objectAtIndex_NSArray0:));
        if (old && new) {
            method_exchangeImplementations(old, new);
        }
        // __NSArrayM
        old = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
        new = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(YSH_objectAtIndex_NSArrayM:));
        if (old && new) {
            method_exchangeImplementations(old, new);
        }
    });
}

// 查
- (id)YSH_objectAtIndex_NSArrayI:(NSUInteger)index {
   if (index > self.count - 1 || !self.count) {
       @try {
           return [self YSH_objectAtIndex_NSArrayI:index];
       } @catch (NSException *exception) {
//           NSLog(@"不可变数组元素越界了");
           return nil;
       } @finally {
       }
   } else {
       return [self YSH_objectAtIndex_NSArrayI:index];
   }
}

- (id)YSH_objectAtIndex_NSSingleObjectArrayI:(NSUInteger)index {
   if (index > self.count - 1 || !self.count) {
       @try {
           return [self YSH_objectAtIndex_NSSingleObjectArrayI:index];
       } @catch (NSException *exception) {
//           NSLog(@"不可数组一个元素越界了");
           return nil;
       } @finally {
       }
   } else {
       return [self YSH_objectAtIndex_NSSingleObjectArrayI:index];
   }
}

- (id)YSH_objectAtIndex_NSArray0:(NSUInteger)index {
   if (index > self.count - 1 || !self.count) {
       @try {
           return [self YSH_objectAtIndex_NSArray0:index];
       } @catch (NSException *exception) {
//           NSLog(@"不可数组0个元素越界了");
           return nil;
       } @finally {
       }
   } else {
       return [self YSH_objectAtIndex_NSArray0:index];
   }
}

- (id)YSH_objectAtIndex_NSArrayM:(NSUInteger)index {
   if (index > self.count - 1 || !self.count) {
       @try {
           return [self YSH_objectAtIndex_NSArrayM:index];
       } @catch (NSException *exception) {
//           NSLog(@"不可数组0个元素越界了");
           return nil;
       } @finally {
       }
   } else {
       return [self YSH_objectAtIndex_NSArrayM:index];
   }
}

// 重写初始化方法,增加非空判断
+ (instancetype)arrayWithObjects:(const id _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt {
    NSUInteger count = 0;
    id _Nonnull __unsafe_unretained const * _Nullable  objPtr = objects;
    NSMutableArray *mArray = [NSMutableArray array];
    for (   ; count < cnt; count ++, objPtr++) {
        if (*objPtr == 0) {
            break;
        }
        [mArray addObject:*objPtr];
    }
    return [[NSArray alloc] initWithArray:mArray];
}

@end
